using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Simple.Controls
{
    public class ImageButton : System.Web.UI.WebControls.ImageButton, IScriptControl
    {
        public string HoverImageUrl
        {
            get
            {
                object value = ViewState["hoverImageUrl"];

                return (value == null) ? string.Empty : (string)value;
            }
            set
            {
                ViewState["hoverImageUrl"] = value;
            }
        }

        //public string Text
        //{
        //    get
        //    {
        //        object value = ViewState["text"];

        //        return (value == null) ? string.Empty : (string)value;
        //    }
        //    set
        //    {
        //        ViewState["text"] = value;
        //    }
        //}


        public string ClientClickFunction
        {
            get
            {
                object value = ViewState["clientClickFunction"];

                return (value == null) ? string.Empty : (string)value;
            }
            set
            {
                ViewState["clientClickFunction"] = value;
            }
        }

        public ImageButton() : base()
        {
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            ScriptManager scriptManager = ScriptManager.GetCurrent(Page);

            if (scriptManager == null)
            {
                throw new InvalidOperationException("ScriptManager required on the page.");
            }

            scriptManager.RegisterScriptControl(this);
        }

        protected override void Render(HtmlTextWriter writer)
        {
            base.Render(writer);

            if (!DesignMode)
            {
                ScriptManager.GetCurrent(this.Page).RegisterScriptDescriptors(this);
            }
        }

        IEnumerable<ScriptDescriptor> IScriptControl.GetScriptDescriptors()
        {
            ScriptControlDescriptor desc = new ScriptControlDescriptor("Simple.Controls.ImageButton", ClientID);

            if (!string.IsNullOrEmpty(HoverImageUrl))
            {
                desc.AddProperty("hoverImageUrl", HoverImageUrl);
            }

            if (!string.IsNullOrEmpty(ClientClickFunction))
            {
                desc.AddEvent("click", ClientClickFunction);
            }

            //if (!string.IsNullOrEmpty(Text))
            //{
            //    desc.AddProperty("text", Text);
            //}

            yield return desc;
        }

        IEnumerable<ScriptReference> IScriptControl.GetScriptReferences()
        {
			//yield return new ScriptReference(Page.ResolveUrl("~/ImageButton.js"));
            yield return new ScriptReference(Page.ResolveUrl(""));
        }
    }
}
